export default class hierProperties {
  constructor() {
    this.properties = []
  }  
  static create() {
    return new hierProperties()
  }
  set(properties) {
    this.properties = properties
  }
  add(tpl=hierProperties.tpl()) {
    this.properties.push(tpl)
  }
  delete(list, key='id') {
    list.forEach(value => {
      let index = this.properties.findIndex(item => item[key] === value);
      if (index !== -1) {
          this.properties.splice(index, 1)
      }
    })
  }
  update(property, key='id') {
    let index = this.properties.findIndex(item => item[key] === property[key]);
    if (index !== -1) {
      this.properties.splice(index, 1, property)
    }
  }
  validName(property) {
    return this.find(property.name, 'name')
  }
  find(value, key='id') {
    return this.properties.find(item => item[key] === value)
  }
  getValues() {
    let values = {}
    this.properties.forEach(item => {
      values[item.name] = item.defaultValue
    })
    return values
  }
  static tpl() {
    return {
      name: '',
      type: '',
      defaultValue: '',
      comments: '',
    }
  }
}